package fr.ensisa.sunspot.actor.osc;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import fr.ensisa.sunspot.ActorManager;

public class TcpServer implements Runnable {
	public static final int DEFAULT_PORT = 7000;
	
	private ActorManager am;
	private ServerSocket server;
	
	public TcpServer(ActorManager am) throws IOException {
		this(am, TcpServer.DEFAULT_PORT);
	}
	
	public TcpServer(ActorManager am, int port) throws IOException {
		this.am = am;
		this.server = new ServerSocket(port);
	}
	
	@Override
	public void run() {
		while(true) {
			try {
				Socket client = this.server.accept();
				this.am.addActor(new OscActor(client));
			}
			catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
